package com.lib.common.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import androidx.annotation.DrawableRes;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lib.common.R;

/**
 * @author 一叶丶枫凌
 * Created on 2016-11-26 20:19
 */
public class ImageTextView extends ConstraintLayout {
    private static final int DEFAULT_COLOR = Color.WHITE;
    public static final int TEXT_LEFT = 0xA;
    public static final int TEXT_TOP = 0xB;
    public static final int TEXT_RIGHT = 0xC;
    public static final int TEXT_BOT = 0xD;

    public static final int WIDGET_TYPE_RADIO = 0x1;
    public static final int WIDGET_TYPE_CHECKED_BOX = 0x2;

    private final TextView mLeftText;
    private final TextView mTopText;
    private final TextView mRightText;
    private final TextView mBotText;

    private OnCheckedListener mOnCheckedListener;
    private OnReCheckedListener mOnRecheckedListener;

    private final View mLl;
    private final ImageView mSrc;
    private boolean mIsChecked;
    private int mWidgetType;

    private int mSrcWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
    private int mSrcHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

    private final int mImgMarginLeft;
    private final int mImgMarginTop;
    private final int mImgMarginRight;
    private final int mImgMarginBot;

    private boolean mIntercept;

    public interface OnCheckedListener {

        /**
         * Called when a view has been clicked.
         *
         * @param v         The view that was clicked.
         * @param isChecked
         */
        void onChecked(ImageTextView v, boolean isChecked);
    }

    public interface OnReCheckedListener {

        /**
         * Called when a view has been clicked again.
         *
         * @param v The view that was clicked.
         */
        void onRechecked(ImageTextView v);
    }

    public void setOnClickListener(OnClickListener l) {
        mIntercept = true;
        super.setOnClickListener(l);
    }

    public void setOnClickListener(int where, OnClickListener l) {
        switch (where) {
            case ImageTextView.TEXT_LEFT:
                mLeftText.setOnClickListener(l);
                break;
            case ImageTextView.TEXT_RIGHT:
                mRightText.setOnClickListener(l);
                break;
            case ImageTextView.TEXT_TOP:
                mTopText.setOnClickListener(l);
                break;
            case ImageTextView.TEXT_BOT:
                mBotText.setOnClickListener(l);
                break;
        }
    }

    public void setOnCheckedListener(OnCheckedListener l) {
        mOnCheckedListener = l;
    }

    public void setOnRecheckedListener(OnReCheckedListener l) {
        mOnRecheckedListener = l;
    }

    public ImageTextView(Context context) {
        this(context, null);
    }

    public ImageTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.ImageTextView);

        mIsChecked = ta.getBoolean(R.styleable.ImageTextView_checked, false);
        mWidgetType = ta.getInt(R.styleable.ImageTextView_widget_type, WIDGET_TYPE_RADIO);
        Drawable img_background = ta.getDrawable(R.styleable.ImageTextView_background);
        Drawable img_src = ta.getDrawable(R.styleable.ImageTextView_src);

        String lText = ta.getString(R.styleable.ImageTextView_leftText);
        String tText = ta.getString(R.styleable.ImageTextView_topText);
        String rText = ta.getString(R.styleable.ImageTextView_rightText);
        String bText = ta.getString(R.styleable.ImageTextView_botText);

        int leftTextSize = ta.getDimensionPixelSize(R.styleable.ImageTextView_leftTextSize, 24);
        int topTextSize = ta.getDimensionPixelSize(R.styleable.ImageTextView_topTextSize, 24);
        int rightTextSize = ta.getDimensionPixelSize(R.styleable.ImageTextView_rightTextSize, 24);
        int botTextSize = ta.getDimensionPixelSize(R.styleable.ImageTextView_botTextSize, 24);

        ColorStateList leftTextColor = ta.getColorStateList(R.styleable.ImageTextView_leftTextColor);
        ColorStateList topTextColor = ta.getColorStateList(R.styleable.ImageTextView_topTextColor);
        ColorStateList rightTextColor = ta.getColorStateList(R.styleable.ImageTextView_rightTextColor);
        ColorStateList botTextColor = ta.getColorStateList(R.styleable.ImageTextView_botTextColor);

        mImgMarginLeft = ta.getDimensionPixelSize(R.styleable.ImageTextView_img_margin_left, -1);
        mImgMarginTop = ta.getDimensionPixelSize(R.styleable.ImageTextView_img_margin_top, -1);
        mImgMarginRight = ta.getDimensionPixelSize(R.styleable.ImageTextView_img_margin_right, -1);
        mImgMarginBot = ta.getDimensionPixelSize(R.styleable.ImageTextView_img_margin_bot, -1);
        mSrcWidth = ta.getDimensionPixelSize(R.styleable.ImageTextView_src_width, mSrcWidth);
        mSrcHeight = ta.getDimensionPixelSize(R.styleable.ImageTextView_src_height, mSrcWidth);
        ta.recycle();

        View view = View.inflate(context, R.layout.view_img_text, this);
        mLl = view.findViewById(R.id.ll_center);
        mSrc = view.findViewById(R.id.nav_src);
        mLeftText = view.findViewById(R.id.nav_left_text);
        mTopText = view.findViewById(R.id.nav_top_text);
        mRightText = view.findViewById(R.id.nav_right_text);
        mBotText = view.findViewById(R.id.nav_bot_text);

        if (img_background != null) {
            mSrc.setBackground(img_background);
        }
        if (img_src != null) {
            mSrc.setImageDrawable(img_src);
        }

        if (img_background != null || img_src != null) {
            LayoutParams params = (LayoutParams) mSrc.getLayoutParams();
            params.width = mSrcWidth;
            params.height = mSrcHeight;
            if (mImgMarginLeft != -1) {
                params.leftMargin = mImgMarginLeft;
            }
            if (mImgMarginTop != -1) {
                params.topMargin = mImgMarginTop;
            }
            if (mImgMarginRight != -1) {
                params.rightMargin = mImgMarginRight;
            }
            if (mImgMarginBot != -1) {
                params.bottomMargin = mImgMarginBot;
            }
            mSrc.setLayoutParams(params);
        } else {
            mSrc.setVisibility(GONE);
        }
        //设置字体大小==============================================
        setTextSize(TEXT_LEFT, TypedValue.COMPLEX_UNIT_PX, leftTextSize);
        setTextSize(TEXT_TOP, TypedValue.COMPLEX_UNIT_PX, topTextSize);
        setTextSize(TEXT_RIGHT, TypedValue.COMPLEX_UNIT_PX, rightTextSize);
        setTextSize(TEXT_BOT, TypedValue.COMPLEX_UNIT_PX, botTextSize);
        //设置颜色==============================================
        if (leftTextColor != null) {
            setTextColor(TEXT_LEFT, leftTextColor);
        } else {
            setTextColor(TEXT_LEFT, DEFAULT_COLOR);
        }
        if (topTextColor != null) {
            setTextColor(TEXT_TOP, topTextColor);
        } else {
            setTextColor(TEXT_TOP, DEFAULT_COLOR);
        }
        if (rightTextColor != null) {
            setTextColor(TEXT_RIGHT, rightTextColor);
        } else {
            setTextColor(TEXT_RIGHT, DEFAULT_COLOR);
        }
        if (botTextColor != null) {
            setTextColor(TEXT_BOT, botTextColor);
        } else {
            setTextColor(TEXT_BOT, DEFAULT_COLOR);
        }
        //设置字符串==============================================
        if (TextUtils.isEmpty(lText)) {
            mLeftText.setVisibility(GONE);
        } else {
            setText(TEXT_LEFT, lText);
        }
        if (TextUtils.isEmpty(tText)) {
            mTopText.setVisibility(GONE);
        } else {
            setText(TEXT_TOP, tText);
        }
        if (TextUtils.isEmpty(rText)) {
            mRightText.setVisibility(GONE);
        } else {
            setText(TEXT_RIGHT, rText);
        }
        if (TextUtils.isEmpty(bText)) {
            mBotText.setVisibility(GONE);
        } else {
            setText(TEXT_BOT, bText);
        }
        setChecked(mIsChecked);
    }

    public void setTextSize(int where, int type, int size) {

        switch (where) {
            case TEXT_LEFT:
                mLeftText.setTextSize(type, size);
                break;
            case TEXT_TOP:
                mTopText.setTextSize(type, size);
                break;
            case TEXT_RIGHT:
                mRightText.setTextSize(type, size);
                break;
            case TEXT_BOT:
                mBotText.setTextSize(type, size);
                break;
        }
    }

    public void setTextColor(int where, int color) {

        switch (where) {
            case TEXT_LEFT:
                mLeftText.setTextColor(color);
                break;
            case TEXT_TOP:
                mTopText.setTextSize(color);
                break;
            case TEXT_RIGHT:
                mRightText.setTextSize(color);
                break;
            case TEXT_BOT:
                mBotText.setTextSize(color);
                break;
        }
    }

    public void setTextColor(int where, ColorStateList color) {

        switch (where) {
            case TEXT_LEFT:
                mLeftText.setTextColor(color);
                break;
            case TEXT_TOP:
                mTopText.setTextColor(color);
                break;
            case TEXT_RIGHT:
                mRightText.setTextColor(color);
                break;
            case TEXT_BOT:
                mBotText.setTextColor(color);
                break;
        }
    }

    public String getText(int where) {

        switch (where) {
            case TEXT_LEFT:
                return mLeftText.getText().toString();
            case TEXT_TOP:
                return mTopText.getText().toString();
            case TEXT_RIGHT:
                return mRightText.getText().toString();
            case TEXT_BOT:
                return mBotText.getText().toString();
        }
        return "";
    }

    public TextView getBtn(int where) {
        switch (where) {
            default:
            case TEXT_LEFT:
                return mLeftText;
            case TEXT_TOP:
                return mTopText;
            case TEXT_RIGHT:
                return mRightText;
            case TEXT_BOT:
                return mBotText;
        }
    }

    public void setText(int where, CharSequence string) {

        switch (where) {
            case TEXT_LEFT:
                if (TextUtils.isEmpty(string)) {
                    mLeftText.setVisibility(GONE);
                } else {
                    mLeftText.setVisibility(VISIBLE);
                    mLeftText.setText(string);
                }
                break;
            case TEXT_TOP:
                if (TextUtils.isEmpty(string)) {
                    mTopText.setVisibility(GONE);
                } else {
                    mTopText.setVisibility(VISIBLE);
                    mTopText.setText(string);
                }
                break;
            case TEXT_RIGHT:
                if (TextUtils.isEmpty(string)) {
                    mRightText.setVisibility(GONE);
                } else {
                    mRightText.setVisibility(VISIBLE);
                    mRightText.setText(string);
                }
                break;
            case TEXT_BOT:
                if (TextUtils.isEmpty(string)) {
                    mBotText.setVisibility(GONE);
                } else {
                    mBotText.setVisibility(VISIBLE);
                    mBotText.setText(string);
                }
                break;
        }
    }

    public void setSrcImageResource(@DrawableRes int resId) {
        mSrc.setImageResource(resId);
        mSrc.setVisibility(VISIBLE);
    }

    public void setSrcBackgroundResource(@DrawableRes int resId) {
        mSrc.setBackgroundResource(resId);
    }

    public void setSrcVisible(boolean visible) {

        mSrc.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setWidgetType(int isSwitcher) {
        mWidgetType = isSwitcher;
    }

    public boolean isChecked() {
        return mIsChecked;
    }

    public void setChecked(boolean b) {
        mIsChecked = b;
        setSelected(b);
        mLl.setSelected(b);
        mSrc.setSelected(b);
        mLeftText.setSelected(b);
        mTopText.setSelected(b);
        mRightText.setSelected(b);
        mBotText.setSelected(b);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mIntercept) {
            super.onTouchEvent(event);
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (mWidgetType == WIDGET_TYPE_RADIO) {
                if (!mIsChecked) {
                    setChecked(true);
                    if (mOnCheckedListener != null) {
                        mOnCheckedListener.onChecked(this, true);
                    }
                } else {
                    if (mOnRecheckedListener != null) {
                        mOnRecheckedListener.onRechecked(this);
                    }
                }
            } else if (mWidgetType == WIDGET_TYPE_CHECKED_BOX) {
                setChecked(!mIsChecked);
                if (mOnCheckedListener != null) {
                    mOnCheckedListener.onChecked(this, mIsChecked);
                }
            }
        }
        return true;
    }

    @Override
    protected void onDetachedFromWindow() {
        mOnCheckedListener = null;
        mOnRecheckedListener = null;
        super.onDetachedFromWindow();
    }
}